<template>
	<view class="container">
		<!-- 头部提示 -->
		<view class="header">
			<view class="notice-bar">
				{{ notice }}
			</view>
		</view>
		<!-- 数据表格 -->
		<view class="content">
			<view class="table-head">
				<u-table>
					<u-tr>
						<u-th v-for="(col,colIndex) in tableDatas.columns" :key="colIndex" :width="col.width">
							<template v-if="col.type==='checkbox'">
								<u-checkbox v-model="tableDatas.selectAll" @click.native="selectAll()"></u-checkbox>
							</template>
							<template v-else>
								{{col.label}}
							</template>
						</u-th>
					</u-tr>
				</u-table>
			</view>
			<view class="table-body">
				<scroll-view scroll-y="true" style="height: 100%;" @scrolltolower="scrolltolower()"
					:lower-threshold="3">
					<u-table font-size="24">
						<u-tr v-for="(row,rowIndex) in tableDatas.datas" :key="rowIndex">
							<u-td v-for="(col,colIndex) in tableDatas.columns" :key="colIndex" :width="col.width"
								:class="`col-${colIndex} ${col.prop==='edit'?'col-edit':''}`">
								<template v-if="col.type==='checkbox'">
									<u-checkbox v-model="row[col.prop]"></u-checkbox>
								</template>
								<template v-else-if="col.type==='input'">
									<u-input v-model="row[col.prop]" :placeholder="col.placeholder||'请输入'"></u-input>
								</template>
								<template v-else-if="col.type === 'switch'">
									<u-switch v-model="row['computed'+col.prop]" :active-value="1" :inactive-value="0"
										size="40" @change="switchChange($event,row,col)"></u-switch>
								</template>
								<template v-else>
									{{ row[col.prop]}}
								</template>
							</u-td>
						</u-tr>
						<u-loadmore :status="tableDatas.loadmoreVisible" @loadmore="loadMore()" color="#3e82f9" />
					</u-table>
				</scroll-view>
			</view>
		</view>
		<!-- 底部数量统计 -->
		<view class="footer">
			<u-button type="success" @click="addNew()" v-if="hasPermission">新增</u-button>
			<u-button type="primary" @click="saveData()" v-if="hasPermission">保存</u-button>
		</view>
	</view>
</template>

<script>
	import myTab from '@/components/myTabs/index.vue'
	export default {
		components: {
			myTab
		},
		data() {
			return {
				form: {
					page: 1,
					rows: 50,
				},
				tableDatas: {
					dicID: 168,
					datas: [],
					columns: [{
							prop: 'isChecked',
							label: '',
							width: '10%',
							type: 'checkbox'
						},
						{
							prop: 'Code',
							label: '储位编码',
							width: '35%',
							type: 'input'
						},
						{
							prop: 'Name',
							label: '储位名称',
							width: '35%',
							type: 'input'
						},
						{
							prop: 'Status',
							label: '是否启用',
							width: '20%',
							type: 'switch'
						}

					],
					formData: {},
					loading: false,
					loadmoreVisible: 'loadmore',
					loadText: '滚动或点击加载更多',
					count: 0,
					selectAll: false
				},
				rowDatas: {},
				notice: '提示:',
				hasPermission: false
			}
		},
		onLoad(options) {
			// 权限管理
			const resulut = this.$store.getters.userInfo.RoleMap?.some(val => val.RoleID === 'E01Admin' || val.RoleID ===
				'R2309260003') // 管理员和仓管人员
			this.$set(this, 'hasPermission', resulut)
			this.rowDatas = options.row ? JSON.parse(options.row) : {}
			const data = this.rowDatas
			this.notice = `提示:${data.WarehouseName1||''},${data.WarehouseName}正在配置储位`
			this.dataSearch()
		},
		methods: {
			// 获取数据
			async getApsData(remark = 0) {
				const condition = {
					dicID: this.tableDatas.dicID,
					WarehouseID: this.rowDatas.WarehouseID
				}
				Object.assign(condition, this.form)
				this.tableDatas.loading = true
				if (this.form.page === 1) {
					this.tableDatas.datas.length = 0
				}
				this.setLoadmore('loading')
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: condition
				}).catch(err => err)
				this.tableDatas.loading = false
				if (!res) return
				const {
					result,
					msg,
					data,
					count
				} = res
				if (!result) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				if (res.result) {
					this.$set(this.tableDatas, 'count', count)
					if (!res.data.length) {
						this.setLoadmore('nomore')
					} else {
						let mapData = res.data.map(item => ({
							...item,
							computedStatus: item.Status === 1
						}))
						this.tableDatas.datas.push(...mapData)
						this.setLoadmore('loadmore')
					}
				} else {
					return Promise.reject()
				}
			},
			// 加载更多
			loadMore() {
				if (this.tableDatas.loadmoreVisible === 'nomore') return
				this.form.page++
				this.getApsData()
			},
			//配置加载更多
			setLoadmore(status = 'loadmore') {
				const textMenu = {
					loadmore: '滑动或点击加载更多',
					loading: '加载中...',
					nomore: '已无更多数据',
				}
				this.$set(this.tableDatas, 'loadmoreVisible', status)
				this.$set(this.tableDatas, 'loadmoreText', textMenu[status])
			},
			// 表格滚动到底部
			scrolltolower() {
				this.loadMore()
			},
			selectAll() {
				this.tableDatas.datas.forEach(val => {
					this.$set(val, 'isChecked', this.tableDatas.selectAll)
				})
			},
			dataSearch() {
				this.form.page = 1
				this.getApsData()
			},
			// 保存
			async saveData() {
				const submintData = this.tableDatas.datas.filter(val => val.isChecked)
				const res = await this.$request.post({
					url: '/APSAPI/SaveData',
					data: submitData
				}).catch(err => err)
				const {
					result,
					msg
				} = res
				if (!result) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				this.dataSearch()
				uni.showModal({
					title: '提示',
					content: msg || '操作成功',
					showCancel: false,
				})
			},
			addNew() {
				const obj = {
					dicID: 168,
					Code: '',
					Name: '',
					Status: 1,
					computedStatus: true, //用作Status组件switch控制
					RowNumber: 1,
					isChecked: true,

				}
				this.tableDatas.datas.unshift(obj)
			},
			switchChange(e, row, col) {
				this.$set(row, col.prop, e)
			},
		}
	}
</script>

<style scoped lang="scss">
	* {
		box-sizing: border-box;
	}

	.container {
		width: 100%;
		height: 100vh;
		overflow: hidden;

		.header {
			height: 50rpx;
			// border-bottom: 1px solid $uni-border-color;
		}

		.notice-bar {
			background-color: #fdf6ec;
			color: $uni-color-warning;
			height: 100%;
			width: 100%;
		}

		.navbar {
			padding: 0 10rpx;
			height: 50rpx;


		}

		.content {
			height: calc(100vh - 261rpx);
			width: 100%;
			// flex: 1;
			display: flex;
			flex-direction: column;
			padding: 10rpx 10rpx 20rpx;
			overflow: hidden;

			.table-head {}

			.table-body {
				flex-grow: 1;
				width: 100%;
				overflow: hidden;

				uni-scroll-view {
					height: 100%;
				}

				.u-table {
					width: 100%;
					overflow-y: auto;
					overflow-x: hidden;
				}
			}

			.u-td,
			.u-th {
				white-space: nowrap !important;
				overflow: hidden !important;
				text-overflow: ellipsis !important;
				min-width: 0 !important;
				/* 关键属性 */
				flex-shrink: 0 !important;
				/* 防止flex容器压缩 */
			}

			.u-td {
				height: 80rpx;
			}

			.col-1 {
				white-space: pre-wrap !important;
				background-color: rgb(251, 255, 202) !important;
			}

			.col-0 {
				// font-size: 18rpx !important;
				// white-space: pre-wrap !important;
				
			}

			.col-4 {
				// color: red !important;
			}

		}

		.footer {
			height: 100rpx;
			padding: 10rpx 20rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
			// padding-right: 20rpx;
		}
	}
</style>